﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Suppliers;
using Traceability.App.API.DTOS;
using Traceability.App.Domain.Entities;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructure.Interface;

namespace Traceability.App.API.Application.Handler.Suppliers
{
    /// <summary>
    /// 根据生产批次获取供应商请求命令处理类
    /// </summary>
    public class GetSupplierHandler:IRequestHandler<GetSupplierCommand,ApiResult<List<SupplierDTO>>>
    {
        IBaseRepository<Supplier> supplierRep;
        IMapper mapper;

        public GetSupplierHandler(IBaseRepository<Supplier> supplierRep, IMapper mapper)
        {
            this.supplierRep = supplierRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<SupplierDTO>>> Handle(GetSupplierCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<SupplierDTO>> res = new ApiResult<List<SupplierDTO>>();

                var supplier = supplierRep.GetAll().Where(x => x.Production_Batch == request.production_Batch && !x.IsDeleted);
                var model = mapper.Map<List<SupplierDTO>>(supplier);
                res.Msg = "根据生产批次获取供应商信息";
                res.Code = ApiEnum.Success;
                res.Data = model;
                return Task.FromResult(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
